<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class Bitrix24Service
{
    protected string $domain;
    protected string $accessToken;

    public function __construct()
    {
        $this->domain = config('bitrix.domain');
        $this->accessToken = config('bitrix.access_token');
    }

    public function restCommand($method, $params = [], $auth = [])
    {
        $url = "https://{$this->domain}/rest/{$method}";
        $params['auth'] = $auth['access_token'];

        $response = Http::post($url, $params);
        $result = $response->json();

        Log::info('Bitrix24 REST Command', [
            'url' => $url,
            'params' => $params,
            'response' => $result
        ]);

        return $result;
    }
}
